using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing;
using Platform.ChooseTextPanel;

namespace Platform.algorithm
{
    class HashTabel
    {
        private static Dictionary<string, Image> images = new Dictionary<string, Image>();
        public static Image GetImage(Element element)
        {
            if (!images.ContainsKey(element.SecondCode))
                images[element.SecondCode] = element.ToImage();
            return images[element.SecondCode];
        }
        public static Image GetBaseImage(string unitCode)
        {
            if (unitCode.Length == 2) unitCode += "0";
            string dickey = unitCode + GlobalSetting.KnowledgeSet.ToString();
            if (!images.ContainsKey(dickey))
            {
                Bitmap bitmap;
                string file = System.Windows.Forms.Application.StartupPath + GlobalSetting.GetBasePath() + unitCode + ".bmp";
                if (!File.Exists(file))
                {                    
                    bitmap = new Bitmap(32, 32);
                    for (int i = 0; i < 32; i++)
                        for (int j = 0; j < 32; j++)
                            bitmap.SetPixel(i, j, Color.White);
                    if (Regex.IsMatch(unitCode, "^[A-Z]$")) return bitmap;
                    Util.Log(unitCode + ".bmp   do not exists.");
                    return bitmap;
                }
                bitmap = (Bitmap)Bitmap.FromFile(file);
                images[dickey] = bitmap;
            }
            return images[dickey];
        }
        public static void Dispose()
        {
            Dictionary<string, Image>.Enumerator enumerator = images.GetEnumerator();
            while (enumerator.MoveNext())
            {
                enumerator.Current.Value.Dispose();
            }
            enumerator.Dispose();
        }
    }
}
